import { IMG_URL } from '@static'
import marked from 'marked'
export function setSession(name, value) {
  sessionStorage.setItem(name, JSON.stringify(value))
}
export function getSession(name) {
  const v = sessionStorage.getItem(name)
  try {
    return v == null ? '' : JSON.parse(v)
  } catch (error) {
    return ''
  }
}
export function pad0(data, len = 2) {
  return ('00000000000' + data).substr(-len)
}
export function deepClone(data = {}) {
  if (typeof data !== 'object' || data === null) {
    return data
  }
  let res
  if (data instanceof Array) {
    res = []
  } else {
    res = {}
  }
  for (const key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      res[key] = deepClone(data[key])
    }
  }
  return res
}
export function convertURL(obj = {}) {
  const arr = []
  for (const key in obj) {
    if (obj[key] && key) {
      arr.push(`${key}=${obj[key]}`)
    }
  }
  return arr.join('&')
}
export function bindIMG(url = 'headimg_placeholder.png') {
  return IMG_URL + url
}
export function checkEmail(rule, value, callback) {
  const reg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/
  if (!reg.test(value)) {
    return callback(new Error('电子邮箱格式错误'))
  } else {
    return callback()
  }
}
export function checkPhone(rule, value, callback) {
  const reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
  if (!reg.test(value)) {
    return callback(new Error('手机号码格式错误'))
  } else {
    return callback()
  }
}
export function checkBM(rule, value, callback) {
  const reg = /^\d{6}$/
  if (!reg.test(value.trim())) {
    return callback(new Error('请输入6位数字作为图书编号'))
  }
  return callback()
}

// done 32dig id
export function getUid(dig = 32) {
  const s = []
  const hexDigits = '0123456789ABCDEF'
  for (var i = 0; i < dig; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4'
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
  return s.join('')
  // return Number(Math.random().toString().substr(2, randomLength) + Date.now()).toString(36)
}

export function markdownToHtml (mark) {
  return marked(mark)
}

export function randomNumber(start, end) {
  return Math.floor(Math.random() * end) + start
}
